package SortExe;

import jdk.nashorn.internal.runtime.regexp.joni.constants.internal.StackPopLevel;

import java.util.ArrayList;
import java.util.Arrays;

public class SelectionSort {
    public static void selectionSort(int[] arr, int l, int r){
        if (arr == null || arr.length <2){
            return;
        }
        for (int i = l; i < r; i++) {
            int minIndex = i;
            for (int j = i; j < r+1; j++) {
                minIndex = arr[minIndex] > arr[j]?j:minIndex;
            }
            SelectionSort.swap(arr,i,minIndex);
        }
    }

    public static void swap(int[] arr,int x,int y){
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {1,4,5,2,7,9};
        selectionSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
}
